//
//  JddBasketBallHHSelectController.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-12-16.
//
//

#import "JddBasketBallHHSelectController.h"
#import "JddViewControllerManager.h"
#import "JddHH_TableView.h"
@interface JddBasketBallHHSelectController ()

@end

@implementation JddBasketBallHHSelectController

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
    [MobClick beginLogPageView:@"JCLQ-hunhewanfa"];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"JCLQ-hunhewanfa"];
}
//- (void)backView
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

- (void)viewDidLoad {
	[super viewDidLoad];
    self.title = @"赛事方案";
    	
    //初始化数据
	[self initData];
	//初始化按钮状态
	[self initBtn];
    
	sfArr = [NSMutableArray arrayWithObject:@"胜负:"];
	rfArr = [NSMutableArray arrayWithObject:@"让分胜负:"];
	dxfArr = [NSMutableArray arrayWithObject:@"大小分:"];
	ksArr = [NSMutableArray array];
	zsArr = [NSMutableArray array];
	resultStr = [NSMutableString string];
	resultArray = [NSMutableArray arrayWithObjects:sfArr, rfArr, dxfArr, ksArr, zsArr, nil];
}

- (void)initData {
	//初始化标题栏
	NSString *wk = [self.arr objectAtIndex:0];
	NSString *num = [self.MatchCell objectForKey:@"MID"];
	num = [NSString stringWithFormat:@"%@", [num substringFromIndex:6]];
	NSString *HTeam = [self.MatchCell objectForKey:@"HTeam"];
	NSString *VTeam = [self.MatchCell objectForKey:@"VTeam"];
    
	NSString *titleStr = [NSString stringWithFormat:@"%@%@  %@VS%@", wk, num, VTeam, HTeam ];
	titleLbl.text = titleStr;
	//胜负
	NSString *spspfStr = [self.MatchCell objectForKey:@"SpSF"];
	if (spspfStr.length > 0) {
		NSArray *SPSPF = [spspfStr componentsSeparatedByString:@"|"];
		sfLbl1.text = [SPSPF objectAtIndex:0];
		sfLbl2.text = [SPSPF objectAtIndex:1];
	}
	else {
		for (UIView *u in self.view.subviews) {
			if ([u isKindOfClass:[UIButton class]]) {
				UIButton *btn = (UIButton *)u;
				if (btn.tag == 1) {
					btn.enabled = NO;
				}
			}
		}
	}
	//让分胜负
	NSString *rfsfStr = [self.MatchCell objectForKey:@"SpRFSF"];
	if (rfsfStr.length > 0) {
		NSArray *rfsf = [rfsfStr componentsSeparatedByString:@"|"];
		[rfsfBtn2 setTitle:[rfsf objectAtIndex:0] forState:UIControlStateNormal];
		rfsfLbl1.text = [rfsf objectAtIndex:1];
		rfsfLbl3.text = [rfsf objectAtIndex:2];
	}
	else {
		for (UIView *u in self.view.subviews) {
			if ([u isKindOfClass:[UIButton class]]) {
				UIButton *btn = (UIButton *)u;
				if (btn.tag == 2) {
					btn.enabled = NO;
				}
			}
		}
	}
    
	//大小分
	NSString *dxfStr = [self.MatchCell objectForKey:@"SpDXF"];
	if (dxfStr.length > 0) {
		NSArray *dxf = [dxfStr componentsSeparatedByString:@"|"];
		[dxfBtn2 setTitle:[dxf objectAtIndex:0] forState:UIControlStateNormal];
		dxfLbl1.text = [dxf objectAtIndex:1];
		dxfLbl3.text = [dxf objectAtIndex:2];
	}
	else {
		for (UIView *u in self.view.subviews) {
			if ([u isKindOfClass:[UIButton class]]) {
				UIButton *btn = (UIButton *)u;
				if (btn.tag == 3) {
					btn.enabled = NO;
				}
			}
		}
	}
	//胜分差
	NSString *sfcStr = [self.MatchCell objectForKey:@"SpSFC"];
	if (sfcStr.length > 0) {
		NSArray *sfc = [sfcStr componentsSeparatedByString:@"|"];
		ksLbl1.text = [sfc objectAtIndex:0];
		ksLbl2.text = [sfc objectAtIndex:1];
		ksLbl3.text = [sfc objectAtIndex:2];
		ksLbl4.text = [sfc objectAtIndex:3];
		ksLbl5.text = [sfc objectAtIndex:4];
		ksLbl6.text = [sfc objectAtIndex:5];
        
		zsLbl1.text = [sfc objectAtIndex:6];
		zsLbl2.text = [sfc objectAtIndex:7];
		zsLbl3.text = [sfc objectAtIndex:8];
		zsLbl4.text = [sfc objectAtIndex:9];
		zsLbl5.text = [sfc objectAtIndex:10];
		zsLbl6.text = [sfc objectAtIndex:11];
	}
	else {
		for (UIView *u in self.view.subviews) {
			if ([u isKindOfClass:[UIButton class]]) {
				UIButton *btn = (UIButton *)u;
				if (btn.tag == 4 || btn.tag == 5) {
					btn.enabled = NO;
				}
			}
		}
	}
}

- (void)initBtn {
	for (UIView *u in self.view.subviews) {
		if ([u isKindOfClass:[UIButton class]]) {
			UIButton *btn = (UIButton *)u;
			if (btn.tag > 0) {
				[btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
				[btn setBackgroundImage:LOADIMAGE(@"sport_select") forState:UIControlStateSelected];
				//设置边框
				[btn.layer setMasksToBounds:YES];
				[btn.layer setBorderWidth:0.5]; //边框宽度
//				CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//				CGColorRef colorref = CGColorCreate(colorSpace, (CGFloat[]) { 0.9294f, 0.5686f, 0.1294f, 1.0f });
				[btn.layer setBorderColor:BORDER_COLOR]; //边框颜色
			}
		}
		if (self.arr.count > 2) {
			NSMutableArray *resultArrs = [self.arr objectAtIndex:2];
			if (resultArrs.count > 0) {
				NSArray *spfA = [resultArrs objectAtIndex:0];
				NSArray *rqspfA = [resultArrs objectAtIndex:1];
				NSArray *zjcA = [resultArrs objectAtIndex:2];
				NSArray *bqcA = [resultArrs objectAtIndex:3];
				NSArray *bfA = [resultArrs objectAtIndex:4];
                
				if ([u isKindOfClass:[UIButton class]]) {
					UIButton *btn = (UIButton *)u;
					switch (btn.tag) {
						case 1:
							[self setBtnSelectedWithArray:spfA AndBtn:btn];
							break;
                            
						case 2:
							[self setBtnSelectedWithArray:rqspfA AndBtn:btn];
							break;
                            
						case 3:
							[self setBtnSelectedWithArray:zjcA AndBtn:btn];
							break;
                            
						case 4:
							[self setBtnSelectedWithArray:bqcA AndBtn:btn];
							break;
                            
						case 5:
							[self setBtnSelectedWithArray:bfA AndBtn:btn];
							break;
                            
						default:
							break;
					}
				}
			}
		}
	}
}

- (void)setBtnSelectedWithArray:(NSArray *)arr AndBtn:(UIButton *)btn {
	if (arr.count > 0) {
		for (NSString *s in arr) {
			NSString *trimmedString = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
			NSString *btnText = [btn.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            
			trimmedString = [trimmedString stringByReplacingOccurrencesOfString:@"客胜("withString:@""];
			trimmedString = [trimmedString stringByReplacingOccurrencesOfString:@"主胜("withString:@""];
			trimmedString = [trimmedString stringByReplacingOccurrencesOfString:@")"withString:@""];
			trimmedString = [trimmedString stringByReplacingOccurrencesOfString:@"其他"withString:@"26+"];
            
			if ([btnText isEqualToString:trimmedString]) {
				[btn setSelected:YES];
			}
		}
	}
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
	if (buttonIndex == 0) {
		[self popWindow];
	}
}

- (IBAction)backAction:(id)sender {
	UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示"
	                                                    message:@"退出后您的选项将不会保存，确定继续？"
	                                                   delegate:self
	                                          cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];    [alertView show];
}

- (void)popWindow {
	JddBasketBall_HH_TableView *h = (JddBasketBall_HH_TableView *)self.fromController;
	NSIndexPath *indexPath = (NSIndexPath *)[self.arr objectAtIndex:1];
	[h sportTeamButtonClicked:indexPath AndResultStr:resultStr AndResultArr:resultArray];
	[[JddViewControllerManager sharedInstance] popToBackViewController];
}

- (IBAction)selectAction:(id)sender {
	UIButton *btn = (UIButton *)sender;
	btn.selected = !btn.selected;
}

- (IBAction)okAction:(id)sender {
	for (UIView *u in self.view.subviews) {
		if ([u isKindOfClass:[UIButton class]]) {
			UIButton *btn = (UIButton *)u;
			int btnTag = btn.tag;
			if (btn.selected) {
				NSString *title = [NSString stringWithFormat:@"%@ ", btn.titleLabel.text];
				switch (btnTag) {
					case 1:
						[sfArr addObject:title];
						break;
                        
					case 2:
						[rfArr addObject:title];
						break;
                        
					case 3:
						[dxfArr addObject:title];
						break;
                        
					case 4:
						title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
						if ([title isEqualToString:@"26+"]) {
							title = @"其他";
						}
						[ksArr addObject:[NSString stringWithFormat:@"客胜(%@) ", title]];
						break;
                        
					case 5:
						title = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
						if ([title isEqualToString:@"26+"]) {
							title = @"其他";
						}
						[zsArr addObject:[NSString stringWithFormat:@"主胜(%@) ", title]];
						break;
                        
					default:
						break;
				}
			}
		}
	}
	if (sfArr.count > 1) {
		NSString *spfStr = [sfArr componentsJoinedByString:@""];
		[resultStr appendString:spfStr];
	}
	if (rfArr.count > 1) {
		NSString *rqspStr = [rfArr componentsJoinedByString:@""];
		[resultStr appendString:rqspStr];
	}
	if (dxfArr.count > 1) {
		NSString *zjcStr = [dxfArr componentsJoinedByString:@""];
		[resultStr appendString:zjcStr];
	}
	BOOL ksArrayExist = NO;
	if (ksArr.count > 0) {
		NSString *bqcStr = [ksArr componentsJoinedByString:@""];
		bqcStr = [NSString stringWithFormat:@"胜分差:%@", bqcStr];
		[resultStr appendString:bqcStr];
		ksArrayExist = YES;
	}
	if (zsArr.count > 0) {
		NSString *bfStr = [zsArr componentsJoinedByString:@""];
		if (!ksArrayExist) {
			bfStr = [NSString stringWithFormat:@"胜分差:%@", bfStr];
		}
		[resultStr appendString:bfStr];
	}
	[self popWindow];
}

@end
